package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.APPUtils;
import com.example.demo.server.common.ArticleUtils;
import com.example.demo.server.common.SessionUtils;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;


public class UpdateView implements View {


    public void showPage(UserInfo userInfo, ArticleInfo articleInfo) {
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]    我的博客（mylist）  主页（list）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.println("+————————————————————————————————————————————————————————————————————————————————+");
        System.out.println("|TITLE: " + articleInfo.getTitle());
        System.out.println("|SUMMARY: " + articleInfo.getSummary());
        System.out.println("+——[CONTENT]—————————————————————————————————————————————————————————————————————+");
        System.out.println(articleInfo.getContent());
        System.out.println("+————————————————————————————————————————————————————————————————————————————————+");
    }

    @Override
    public CommonRequest jump(CommonRequest request) {
        UserInfo userInfo = SessionUtils.getUser();
        if(userInfo == null) {
            APPUtils.printPopUp("Please Login!");
            return CommonRequest.createRequest(ViewKey.LOGIN);
        }
        ArticleInfo articleInfo;
        MyHashMap<String, Object> map = (MyHashMap<String, Object>) request.getBody();
        Boolean flag = (Boolean) map.get("submit");
        if(flag) {
            APPUtils.printPopUp("Update Success!");
            return CommonRequest.createRequest(ViewKey.MY_LIST);
        }
        articleInfo = (ArticleInfo) map.get("art");
        showPage(userInfo, articleInfo);
        String command = APPUtils.input();
        MyList<String> myList = APPUtils.handleCommand(command);
        if(myList.size() == 2 && myList.get(0).equals(ViewKey.UPDATE)) {
            String opera = myList.get(1).toLowerCase();
            MyHashMap<String, Object> retMap = new MyHashMap<>();
            switch (opera) {
                case "title":
                    ArticleUtils.setTittle(articleInfo);
                    break;
                case "summary":
                    ArticleUtils.setSummary(articleInfo);
                    break;
                case "content":
                    ArticleUtils.setContent(articleInfo);
                    break;
                case "submit":
                    flag = ArticleUtils.update(articleInfo);
                    break;
                default:
                    APPUtils.printPopUp("ERROR: Please Again!");
                    break;
            }
            retMap.put("submit", flag);
            retMap.put("art", articleInfo);
            return CommonRequest.createRequest(ViewKey.UPDATE, retMap);
        }else if(myList.size() == 1) {
            String view = myList.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view,
                    ViewKey.LOGOUT, ViewKey.MY_LIST, ViewKey.LIST, ViewKey.UPDATE);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again!");
        return CommonRequest.createRequest(ViewKey.UPDATE, request.getBody());
    }
}
